package com.nf.teamwork.mall.config.security.api;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.nf.teamwork.mall.vo.ResponseVO;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: LJP
 * @Classname JwtAuthEntryPoint
 * @Date: 2020-03-01 19:10
 * @Description: 认证不通过处理类
 */
public class JwtAuthEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        ResponseVO responseVO = new ResponseVO();
        responseVO.setCode(httpServletResponse.SC_FORBIDDEN); //403表示未授权
        responseVO.setMessage("是否拥有一定权限？账号是否激活？账号密码输入是否正确？");
        ObjectMapper objectMapper = new ObjectMapper();
        httpServletResponse.setContentType("application/json;charset=UTF-8");
        objectMapper.writeValue(httpServletResponse.getOutputStream(), responseVO);
    }
}
